#include "sys_uart.h"

uint8_t  UART_RX_BUF[USART_REC_LEN]={0};
static uint16_t UART_RX_STA=0;
static uint32_t wlen = 0;
BOOL USART_RX_GET_FLAG = FALSE;

#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
void _sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
int fputc(int ch,FILE *p)  //fputc  函数重定义
{
    USART_SendData(USART1,(u8)ch);	
    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
    return ch;
}

void sys_uart_init(BAUDRATE boundrate)
{
    GPIO_InitTypeDef GPIOInitStructre;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    SYS_USMART_GPIO_PeriphClockFunc(RCC_APB2Periph_GPIOA,ENABLE);
    GPIOInitStructre.GPIO_Pin = SYS_USMART_GPIO_TXD;
    GPIOInitStructre.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIOInitStructre.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SYS_USMART_GPIO_PORT,&GPIOInitStructre);
    GPIOInitStructre.GPIO_Pin = SYS_USMART_GPIO_RXD;
    GPIOInitStructre.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SYS_USMART_GPIO_PORT,&GPIOInitStructre);

    SYS_USMART_PeriphClockFunc(SYS_USMART_PeriphClock,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    USART_InitStructure.USART_BaudRate = boundrate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(SYS_USMART_USARTx, &USART_InitStructure);

    USART_ITConfig(SYS_USMART_USARTx, USART_IT_RXNE, ENABLE);
    USART_ITConfig(SYS_USMART_USARTx, USART_IT_IDLE, ENABLE);
    USART_Cmd(SYS_USMART_USARTx, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = SYS_USMART_USARTx_IRQ;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void SYS_USMART_USARTx_IRQ_Handler(void)
{
    if(USART_GetITStatus(SYS_USMART_USARTx, USART_IT_RXNE) != RESET)
    {
        UART_RX_BUF[UART_RX_STA++] = USART_ReceiveData(SYS_USMART_USARTx);
    }
    if(USART_GetITStatus(SYS_USMART_USARTx, USART_IT_IDLE) != RESET)
    {
        SYS_USMART_USARTx -> SR;
        SYS_USMART_USARTx -> DR;
        wlen = UART_RX_STA;
        UART_RX_STA -= 1;
        UART_RX_BUF[UART_RX_STA] = '\0';
        if((wlen == 1) && UART_RX_BUF[UART_RX_STA] == 0)
        {
            DBG_MSG("#\r\n");
        }
        UART_RX_STA = 0;
        USART_RX_GET_FLAG = TRUE;
        // USART_ClearITPendingBit(SYS_USMART_USARTx,USART_IT_RXNE);
        // USART_ClearITPendingBit(SYS_USMART_USARTx,USART_IT_IDLE);
    }
}

uint8_t sys_usmart_getstring(uint8_t *rx_str)
{
    int i = 0;
    if(USART_RX_GET_FLAG == TRUE)
    {
        memcpy(rx_str,UART_RX_BUF,wlen);
        USART_RX_GET_FLAG = FALSE;
    }
    else
    {
        wlen = 0;
        memset(rx_str,0,USART_REC_LEN);
        memset(UART_RX_BUF,0,USART_REC_LEN);
    }
    if((wlen > 0)&&(hdl_cmd_debug_flag == TRUE))
    {
        DBG_MSG("get len:%d dat:",wlen);
        for (i = 0; i < wlen;)
        {
            DBG_MSG("0x%02x 0x%02x 0x%02x 0x%02x\r\n",UART_RX_BUF[i],UART_RX_BUF[i+1],UART_RX_BUF[i+2],UART_RX_BUF[i+3]);
            i+=4;
        }
    }
    return wlen;
}
